package com.example.springbootquartz.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootquartz.service.JobService;
import com.example.springbootquartz.vo.JobInfo;
import com.example.springbootquartz.vo.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhanpengguo
 * @date 2019-08-22 9:18
 */
@RestController
@RequestMapping("job")
public class JobController {

    Logger logger = LoggerFactory.getLogger(JobController.class);

    @Autowired
    JobService jobService;

    @PostMapping("/jobInfo")
    public JsonResult addJob(@RequestBody JobInfo jobInfo) {
        logger.info("添加请求参数：{}",JSONObject.toJSONString(jobInfo));
        try {
            jobService.addJob(jobInfo);
        } catch (Exception e) {
            logger.error("添加失败",e);
            return new JsonResult(new Throwable(e));
        }
        return new JsonResult("添加成功");
    }

    @PostMapping("/pauseJob")
    public JsonResult pauseJob(@RequestBody JobInfo jobInfo) {
        logger.info("暂停请求参数：{}",JSONObject.toJSONString(jobInfo));
        try {
            jobService.pauseJob(jobInfo);
        } catch (Exception e) {
            logger.error("暂停失败",e);
            return new JsonResult(new Throwable(e));
        }
        return new JsonResult("暂停成功");
    }

    @PostMapping("/resumeJob")
    public JsonResult resumeJob(@RequestBody JobInfo jobInfo) {
        logger.info("恢复请求参数：{}",JSONObject.toJSONString(jobInfo));
        try {
            jobService.resumeJob(jobInfo);
        } catch (Exception e) {
            logger.error("恢复失败",e);
            return new JsonResult(new Throwable(e));
        }
        return new JsonResult("恢复成功");
    }

    @PutMapping("/jobInfo")
    public JsonResult rescheduleJob(@RequestBody JobInfo jobInfo) {
        logger.info("更新请求参数：{}",JSONObject.toJSONString(jobInfo));
        try {
            jobService.rescheduleJob(jobInfo);
        } catch (Exception e) {
            logger.error("更新失败",e);
            return new JsonResult(new Throwable(e));
        }
        return new JsonResult("更新成功");
    }

    @DeleteMapping("/jobInfo")
    public JsonResult deleteJob(@RequestBody JobInfo jobInfo) {
        logger.info("删除请求参数：{}",JSONObject.toJSONString(jobInfo));
        try {
            jobService.deleteJob(jobInfo);
        } catch (Exception e) {
            logger.error("删除失败",e);
            return new JsonResult(new Throwable(e));
        }
        return new JsonResult("删除成功");
    }

    @GetMapping("/jobInfo")
    public JsonResult queryJob() {
        logger.info("查询请求");
        try {
            return new JsonResult(jobService.queryJob());
        } catch (Exception e) {
            logger.error("删除失败",e);
            return new JsonResult(new Throwable(e));
        }
    }
}
